﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLTSCD
{
    public partial class frmPB : DevComponents.DotNetBar.Office2007Form
    {
        int maThaoTac = 0;
        int dong = 0;
        public frmPB()
        {
            InitializeComponent();
        }

        private void grvPB_Load()
        { 
            QLTSCDDataContext db = new QLTSCDDataContext();
            var listPB = from pb in db.PhongBans
                         select pb;
            grvPB.AutoGenerateColumns = false;
            grvPB.DataSource = listPB;
        }
        private void grvPB_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong = e.RowIndex;
            QLTSCDDataContext db = new QLTSCDDataContext();
            int ID = int.Parse(grvPB.Rows[dong].Cells[0].Value.ToString());
            var PB = (from pban in db.PhongBans
                       where pban.MaPhongBan == ID
                       select pban).Single();
            txtSoNhanVien.Text = PB.SoNhanVien.ToString();
            txtTenPhongBan.Text = PB.TenPhongBan;

        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            maThaoTac = 1;
            txtMaPhongBan.Text = "";
            txtSoNhanVien.Text = "";
            txtTenPhongBan.Text = "";

            txtTenPhongBan.Enabled = true;
            txtSoNhanVien.Enabled = true;
            txtMaPhongBan.Enabled = true;
        }

        private void btnThaoTac_Click(object sender, EventArgs e)
        {
            QLTSCDDataContext db = new QLTSCDDataContext();
            string message = "";
            bool flag = false;
            string tenPB = txtTenPhongBan.Text;
            int soNV = int.Parse(txtSoNhanVien.Text.ToString());
            switch (maThaoTac)
            {
                case 0:
                    MessageBox.Show("Bạn chưa chọn thao tác");
                    break;
                case 1:
                    
                    var listPB = from pb in db.PhongBans
                                 where pb.TenPhongBan.Contains(tenPB)
                                 select pb;
                    grvPB.DataSource = listPB;
                    break;
                case 2:
                    PhongBan phongban = new PhongBan();
                    phongban.TenPhongBan = tenPB;
                    phongban.SoNhanVien = soNV;
                    db.PhongBans.InsertOnSubmit(phongban);
                    
                    try
                    {
                        db.SubmitChanges();
                        flag = true;
                    }
                    catch (Exception ex)
                    {
                        flag = false;
                        message = ex.Message;
                    }
                    if (flag)
                    {
                        MessageBox.Show("Bạn đã thêm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("Lỗi" + message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    grvPB_Load();
                    break;
                case 3:
                    int maPB = int.Parse(grvPB.Rows[dong].Cells[0].Value.ToString());
                    PhongBan phongban3 = db.PhongBans.Single(phongban2 => phongban2.MaPhongBan == maPB);
                    if (txtTenPhongBan.Text.Trim() == "")
                    {
                        MessageBox.Show("Cần nhập tên phòng ban!");
                        txtTenPhongBan.Focus();
                    }
                    else
                    {
                        phongban3.TenPhongBan = txtTenPhongBan.Text;
                        phongban3.SoNhanVien = int.Parse(txtSoNhanVien.Text.ToString());
                        try
                        {
                            db.SubmitChanges();
                            flag = true;
                        }
                        catch (Exception ex)
                        {
                            flag = false;
                            message = ex.Message;
                        }
                        if (flag)
                        {
                            MessageBox.Show("Bạn đã sửa thành công!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            MessageBox.Show("Lỗi: " + message, "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        grvPB_Load();
                    }
                    break;
                case 4:
                    int MaPBX = int.Parse(grvPB.Rows[dong].Cells[0].Value.ToString());
                    PhongBan phongbanx = db.PhongBans.Single(phongban1 => phongban1.MaPhongBan == MaPBX);
                    try
                    {
                        DialogResult result = MessageBox.Show("Bạn có chắc xóa hay không?", "Thông báo!", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                        if (result == DialogResult.OK)
                        {
                            db.PhongBans.DeleteOnSubmit(phongbanx);
                            db.SubmitChanges();
                            MessageBox.Show("Bạn đã xóa thành công!");
                        }
                    }
                    catch
                    {

                    }
                    grvPB_Load();
                    break;

                    txtMaPhongBan.Enabled = false;
                    txtSoNhanVien.Enabled = false;
                    txtTenPhongBan.Enabled = false;
            }
        }

        private void frmPB_Load(object sender, EventArgs e)
        {
            grvPB_Load();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            maThaoTac = 2;
           
            txtTenPhongBan.Text = "";
            txtSoNhanVien.Text = "";
            txtMaPhongBan.Text = "";

            txtMaPhongBan.Enabled = false;
            txtSoNhanVien.Enabled = true;
            txtTenPhongBan.Enabled = true;
            txtTenPhongBan.Focus();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            maThaoTac = 3;
            txtMaPhongBan.Enabled = false;
            txtTenPhongBan.Enabled = true;
            txtSoNhanVien.Enabled = true;
            txtTenPhongBan.Focus();
        }

        private void txtSoNhanVien_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            maThaoTac = 4;
           
        }

       
        
    
    }
}
